package com.bootcamp.javase2.stream;

import com.bootcamp.javase2.stream.fileutils.FileUtils;

import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @program: CharacterStreamCode
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-10 20:24
 **/

public class CopyJavaFileRecur {
    final static String DIRE_SRC = "../../StudentManageSystem/little-practice";
    final static String DIRE_DST = "../java_file_collection/";
    final static String FILE_TYPE = ".java";
    static int fileCount = 0;

    public static void main(String[] args) throws IOException {

        // make sure src directory exists
        if (!new File(DIRE_SRC).exists()) return;
        //print directory contents recursively
        FileUtils.printDirResur(new File(DIRE_SRC));

        // copy file to dst recursively
        long start = System.currentTimeMillis();
//        CopyRecursive(new File(DIRE_SRC));
        CopyStack(new File(DIRE_SRC));
//        CopyQueue(new File(DIRE_SRC));
        long end = System.currentTimeMillis();
        System.out.println("copy " + fileCount
                + " files, cost " + (end - start) + " ms");

    }

    private static void CopyRecursive(File file) throws IOException {
        File[] files = file.listFiles();
        if (files == null) {
            if (file.getName().endsWith(FILE_TYPE)) {
                CopySingleFile(file);
                fileCount++;
            }
            return;
        }
        if (files.length == 0) return;
        for (File f : files) {
            CopyRecursive(f);
        }
    }

    private static void CopyStack(File file) throws IOException {
        fileCount = 0;
        Stack<File> stackFile = new Stack<>();
        stackFile.push(file);

        while (!stackFile.empty()) {
            File dir = stackFile.pop();
            File[] files = dir.listFiles();
            if (files == null || files.length == 0) continue;
            for (File f : files) {
                if (f.isFile()) {
                    if (f.getName().endsWith(FILE_TYPE)) {
                        System.out.println(f.getPath());
                        CopySingleFile(f);
                        fileCount++;
                    }
                } else
                    stackFile.push(f);
            }
        }
    }

    private static void CopyQueue(File file) throws IOException {
        fileCount = 0;
        Queue<File> fileQueue = new LinkedList<>();
        fileQueue.offer(file);
        File dir;
        while ((dir = fileQueue.poll()) != null) {
            File[] files = dir.listFiles();
            if (files == null || files.length == 0) continue;
            for (File f : files) {
                if (f.isFile()) {
                    if (f.getName().endsWith(FILE_TYPE)) {
                        System.out.println(f.getPath());
                        CopySingleFile(f);
                        fileCount++;
                    }
                } else
                    fileQueue.offer(f);
            }
        }
    }

    private static void CopySingleFile(File file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        // pay attention to file with same name
        String newFileName = DIRE_DST
                + file.getName().replace(FILE_TYPE, "") + "-"
                + System.currentTimeMillis() % 1000 + FILE_TYPE;
        BufferedWriter writer = new BufferedWriter(new FileWriter(newFileName));
        String sline;
        while ((sline = reader.readLine()) != null) {
            writer.write(sline);
            writer.newLine();
        }
        //close
        reader.close();
        writer.close();
    }
}
